package android.slc.box.commonlibrary.compat;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @author slc
 * @date 2022/09/19 下午 6:41
 */
public class SlcPathUtils {

    private static String ROOT_DIR = System.getProperty("user.dir");
    private static String BASE_DIR = "data";

    public static void setRootDir(String rootDir) {
        if (StringUtils.isEmpty(rootDir)) {
            throw new RuntimeException("rootDir不能为空");
        }
        ROOT_DIR = rootDir;
    }

    public static String getRootDir() {
        return ROOT_DIR;
    }

    public static void setBaseDir(String baseDir) {
        if (StringUtils.isEmpty(baseDir)) {
            throw new RuntimeException("baseDir不能为空");
        }
        BASE_DIR = baseDir;
    }

    public static String getBaseDir() {
        return BASE_DIR;
    }

    public static String getAppPath() {
        //return System.getProperty("user.home") + File.separator + ConstantsBase.PACKAGE_NAME;
        String appPath = getRootDir() + File.separator + BASE_DIR;
        FileUtil.mkdir(appPath);
        return appPath;
    }

    //*************************************  cache  *************************//
    public static String getAppCachePath() {
        String appCachePath = getAppPath() + File.separator + "cache";
        FileUtil.mkdir(appCachePath);
        return appCachePath;
    }

    /**
     * 创建缓存文件
     *
     * @param path
     * @return
     */
    public static File createAppCacheFile(String... path) {
        return FileUtil.file(FileUtil.file(getAppCachePath()), path);
    }

    //*************************************  templates  *************************//

    /**
     * 获取模板文件
     *
     * @return
     */
    public static String getTemplatesPath() {
        File templatesFile = FileUtil.mkdir(createAppCacheFile("templates").getAbsolutePath());
        return templatesFile.getAbsolutePath();
    }

    /**
     * 创建缓存文件
     *
     * @param path
     * @return
     */
    public static File createTemplatesFile(String... path) {
        return FileUtil.file(FileUtil.file(getTemplatesPath()), path);
    }

    /**
     * 根据文件模版获取路径
     *
     * @param filePathPart
     * @return
     */
    public static String getFileByTemplatesPath(String filePathPart) {
        return getTemplatesPath() + File.separator + filePathPart;
    }

    //*************************************  db  *************************//
    public static String getAppDbPath() {
        String appDbPath = getAppPath() + File.separator + "db";
        FileUtil.mkdir(appDbPath);
        return appDbPath;
    }

    /**
     * 创建appDb文件
     *
     * @param path
     * @return
     */
    public static File createAppDbFile(String... path) {
        return FileUtil.file(FileUtil.file(getAppDbPath()), path);
    }

    //*************************************  file  *************************//
    public static String getAppFilePath() {
        String appFilePath = getAppPath() + File.separator + "file";
        FileUtil.mkdir(appFilePath);
        return appFilePath;
    }

    public static File createAppFileFile(String... path) {
        return FileUtil.file(FileUtil.file(getAppFilePath()), path);
    }

    public static String getAppFileFilePath(String... path) {
        return createAppFileFile(path).getAbsolutePath();
    }

    /**
     * 根据AppFile获取相对路径
     *
     * @param absolutePath
     * @return
     */
    public static String appFile2RelativePath(String absolutePath) {
        return StringUtils.remove(absolutePath, getAppFilePath());
    }

    /**
     * 根据AppFile获取绝对路径
     *
     * @param relativePath
     * @return
     */
    public static String relativePath2AppFile(String relativePath) {
        if (!StringUtils.startsWith(relativePath, File.separator)) {
            relativePath = File.separator + relativePath;
        }
        return getAppFilePath() + relativePath;
    }

    public static String path2Url(File file) {
        return file.toURI().toString();
    }

    public static String path2Url(String filePath) {
        return path2Url(FileUtil.file(filePath));
    }

}
